package com.chcit.mobile.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chcit.custom.view.loader.LoaderStyle;
import com.chcit.custom.view.loader.QuickLoader;
import com.chcit.custom.view.text.AutofitTextView;
import com.chcit.mobile.R;
import com.chcit.mobile.app.base.SupportToolbarFragement;
import com.chcit.mobile.di.component.DaggerInventoryPlanHandleComponent;
import com.chcit.mobile.di.module.InventoryPlanHandleModule;
import com.chcit.mobile.mvp.contract.InventoryPlanHandleContract;
import com.chcit.mobile.mvp.model.entity.StorageBean;
import com.chcit.mobile.mvp.presenter.InventoryPlanHandlePresenter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import butterknife.BindView;
import butterknife.OnClick;
import static com.jess.arms.utils.Preconditions.checkNotNull;


public class InventoryPlanHandleFragment extends SupportToolbarFragement<InventoryPlanHandlePresenter> implements InventoryPlanHandleContract.View {

    @BindView(R.id.tv_inventory_handle_index)
    TextView tvIndex;
    @BindView(R.id.tv_inventory_handle_productName)
    TextView tvProductName;
    @BindView(R.id.tv_inventory_handle_productSpec)
    AutofitTextView tvProductSpec;
    @BindView(R.id.tv_inventory_handle_DocumentNo)
    TextView tvDocumentNo;
    @BindView(R.id.tv_inventory_handle_productCode)
    TextView tvProductCode;
    @BindView(R.id.tv_inventory_handle_uom)
    TextView tvUom;
    @BindView(R.id.tv_inventory_handle_lot)
    TextView tvLot;
    @BindView(R.id.tv_inventory_handle_guaranteeDate)
    TextView tvGuaranteeDate;
    @BindView(R.id.tv_inventory_handle_locator)
    TextView tvLocator;
    @BindView(R.id.tv_inventory_handle_BaseUOMQty)
    TextView tvBaseUOMQty;
    @BindView(R.id.tv_inventory_handle_manufacturer)
    AutofitTextView tvManufacturer;
    @BindView(R.id.tv_inventory_handle_storageStatus)
    TextView tvStorageStatus;
    @BindView(R.id.tv_inventory_handle_vendor)
    TextView tvVendor;
    @BindView(R.id.tv_inventory_handle_BaseQtyBook)
    TextView tvBaseQtyBook;
    @BindView(R.id.tv_inventory_handle_qtyBook)
    TextView tvQtyBook;
    @BindView(R.id.tv_inventory_handle_BaseQtyCount)
    AppCompatEditText tvBaseQtyCount;
    @BindView(R.id.et_inventory_handle_QtyCount)
    AppCompatEditText etQtyCount;
    @BindView(R.id.ll_movenent_number)
    LinearLayout llMovenentNumber;
    @BindView(R.id.bt_inventory_handle_cancel)
    Button btCancel;
    @BindView(R.id.bt_inventory_handle_ok)
    Button btOk;
    @BindView(R.id.buttom_bar_group)
    LinearLayout buttomBarGroup;
    private StorageBean storageBean = null;

    public static InventoryPlanHandleFragment newInstance(StorageBean storageBean) {
        InventoryPlanHandleFragment fragment = new InventoryPlanHandleFragment();
        fragment.storageBean = storageBean;
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerInventoryPlanHandleComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .inventoryPlanHandleModule(new InventoryPlanHandleModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_inventory_plan_handle, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mCommonToolbar.setTitle("盘点处理");
        mCommonToolbar.getViewByTag(1).setOnClickListener(l -> {
            pop();
        });
        initShowData();
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        QuickLoader.showLoading(mContext,LoaderStyle.SemiCircleSpinIndicator);
    }

    @Override
    public void hideLoading() {
        QuickLoader.stopLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarTextWithLong(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
       pop();
    }


    @OnClick({R.id.bt_inventory_handle_cancel, R.id.bt_inventory_handle_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_inventory_handle_cancel:
                pop();
                break;
            case R.id.bt_inventory_handle_ok:
             /*   lineData:[{"M_InventoryPlan_ID":1000050,
             "StorageStatus":"S",
             "M_Locator_ID":1013815,
             "M_Attributesetinstance_ID":1066957,
             "M_Product_ID":1099288,
             "BaseUOMQty":1,
             "QtyCount":140,
             "BaseQtyCount":60}]
            appSessionID:1542761758176.4*/

                JSONObject requestData = new JSONObject();
                JSONArray lineData = new JSONArray();
                JSONObject data = new JSONObject();
                data.put("M_InventoryPlan_ID", storageBean.getM_InventoryPlan_ID());
                data.put("StorageStatus", storageBean.getStorageStatus());
                data.put("M_Locator_ID", storageBean.getM_Locator_ID());
                data.put("M_Attributesetinstance_ID", storageBean.getM_Attributesetinstance_ID());
                data.put("BaseUOMQty", storageBean.getBaseUOMQty());
                data.put("M_Product_ID", storageBean.getM_Product_ID());
                if(!etQtyCount.getText().toString().isEmpty()){
                    data.put("QtyCount", etQtyCount.getText().toString());
                }else{
                    ArmsUtils.snackbarTextWithLong("实存数量不能为空");
                }

                if(!tvBaseQtyCount.getText().toString().isEmpty()){
                    data.put("BaseQtyCount", tvBaseQtyCount.getText().toString());
                }else{
                    ArmsUtils.snackbarTextWithLong("实存最小数量不能为空");
                }
                lineData.add(data);
                requestData.put("lineData",lineData);
                mPresenter.doConfirm(requestData, () -> {
                    showMessage("操作成功！");
                    setFragmentResult(RESULT_OK,null);
                    pop();
                });
                break;
        }
    }

    private void initShowData() {
        mCommonToolbar.getViewByTag(1).setOnClickListener(l -> {
            pop();

        });
        tvIndex.setText(storageBean.getProductName());
        tvDocumentNo.setText(storageBean.getDocumentNo());
        tvProductName.setText(storageBean.getProductName());
        tvProductSpec.setText(storageBean.getProductSpec());
        tvManufacturer.setText(storageBean.getManufacturer());
        tvProductCode.setText(storageBean.getProductCode());
        tvUom.setText(storageBean.getUOMName());
        tvLot.setText(storageBean.getLot());
        tvGuaranteeDate.setText(storageBean.getGuaranteeDate());
        tvVendor.setText(storageBean.getVendorName());
        tvBaseQtyCount.setText((storageBean.getBaseQtyCount() + ""));
        tvLocator.setText(storageBean.getLocatorValue());
        tvBaseUOMQty.setText(String.valueOf(storageBean.getBaseUOMQty()));
        tvStorageStatus.setText(storageBean.getStorageStatusName());
        tvBaseQtyBook.setText(String.valueOf(storageBean.getBaseQtyBook()));
        tvQtyBook.setText(String.valueOf(storageBean.getQtyBook()));
        etQtyCount.setText(String.valueOf(storageBean.getQtyCount()));
        /* etInvoiceNo.setText(storageBean.getTaxInvoiceNo());*/
        etQtyCount.setOnFocusChangeListener((v, hasFocus) -> {
            if(!hasFocus&&etQtyCount.getText().toString().isEmpty()){
                etQtyCount.setText(String.valueOf(storageBean.getQtyCount()));
            }
        });
        tvBaseQtyCount.setOnFocusChangeListener((v, hasFocus) -> {
            if(!hasFocus&&tvBaseQtyCount.getText().toString().isEmpty()){
                tvBaseQtyCount.setText(String.valueOf(storageBean.getBaseQtyCount()));
            }
        });

    }

}
